﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;
using ProtoBuf.Message;

namespace Room
{
    public class RCUIFieldBoss : RCUIBase
    {
        public RCUIFieldBoss(RoomCharactor owner)
            : base(owner)
        {
            this.owner = owner;
            type = UIType.ServerMonster;
            //ClickRange = 3f;
            dis = 3f;
        }

        SideBoss boss;
        float timer = 1f;
        float curTime = 0f;
        float dis = 0.8f;
        

        public void Init(SideBoss boss)
        {
            this.boss = boss;
        }



        public override void Tick()
        {
            base.Tick();
            curTime += Time.deltaTime;
            if(curTime >= timer)
            {
                curTime = 0f;

                var self = owner.charactorManager.GetSelfCharactor();
                if (self != null)
                {
                    var selfAI = self.ai as RCAISelf;
                    if (self != null && !selfAI.AutoPathFinding)
                    {
                        var _dis = Vector3.Distance(owner.transform.position, self.transform.position);
                        if (_dis < dis)
                        {
                            OnClick();
                        }
                    }
                }
            }
            
        }

        protected override void OnClick()
        {
            var ai = owner.ai as RCAIFieldBoss;
            var self = owner.charactorManager.GetSelfCharactor();
            if(self!=null)
            {
                var ai0 = self.ai as RCAIActor;
                if(ai0!=null)
                {
                    if (ai.curState == FieldBossAIState.Normal 
                        && ai0.curState == PlayerAIState.Normal )
                    {
                        var state = ai0.GetCurrState() as PlayerAIStateNormal;
                        if (state.InvincibleTimer <= 0)
                        {
                            var room = RoomManager.currentRoom as RoomField;
                            if (room != null)
                            {
                                StaticData.currClickBoss = boss;
                                var netManager = room.netManager as RoomNetField;
                                netManager.SendPveBoss(boss.uuid);
                            }
                        }
                    }
                }
            }
        }
    }
}
